<script src="<?php echo base_url('assets/js/admin/pagination.js'); ?>" type="text/javascript"></script>
<script src="<?php echo base_url('assets/js/admin/load.js'); ?>" type="text/javascript"></script>
<?php
if ($this->uri->segment(4) == '') {
    $editar = false;
} else {
    $editar = true;
}
$id_objeto = $this->uri->segment(4);
?>
<div class="conteudo">
    <div class="cabecalho">
        <h3>Posts</h3>
    </div>
    <div class="inner">
        <form action="<?php echo site_url('admin/post/editar'); ?>" method="POST">
            <div class="form-wrapper">
                <input type="submit" value="Novo post" class="form-submit">
            </div>
            <br/>
            
            <?php
            echo "<div>";
            echo form_label("Menu Principal");
            echo "</div>";
            $c = new Post();
            echo "<div class='overflow;'>";
            echo form_dropdown('id_menu_principal', $c->getMenusPrincipais(), $id_menu_principal, 'id="id_menu_principal" class="left"');
            echo "<div style='float:left;' id='container-load'></div><br style='clear:both;'/><br/>";
            echo "</div>";
            echo "<div>";
            echo form_label("Menu");
            echo "</div>";
            echo "<div class='overflow;'>";
            echo form_dropdown('menu', $c->getMenus($id_menu_principal), $id_menu, 'id="menu"');
            echo "</div>";
            ?>
            <div id="div-resultado">
                <?php
                if (!empty($lista)) {
                    ?>

                    <table class="main-table">
                        <thead>
                            <tr>
                                <th>Ativo</th>
                                <th>Título</th>
                                <th>Ações</th>
                            </tr>
                        </thead>

                        <tbody>
                            <?php
                            foreach ($lista as $item) {
                                printf('<tr>
                                    <td style="width:15px; text-align:center;">%s</td>
                            <td>%s</td>
                            <td style="width:70px;text-align:center;">
                                <a href="%s">editar</a>
                            </td>
                        </tr>', $item->get_ativo() == 1 ? 'Sim' : 'Não', $item->get_titulo(), site_url('admin/post/editar/' . $item->get_id_post()));
                            }
                            ?>
                        </tbody>
                    </table>

                    <?php
                    $config['uri_segment'] = 5;
                    $config['total_rows'] = $total;
                    $config['per_page'] = LIMITE_PAGINACAO_ADMIN;
                    $config['base_url'] = site_url('admin/post/index/' . $id_objeto);
                    $config['use_page_numbers'] = TRUE;
                    $this->pagination->initialize($config);
                    printf('<div class="item-list">
                                <ul class="pager">
                                    %s 
                                </ul>
                            </div>'
                            , $this->pagination->create_links()
                    );
                } else {
                    echo '<p class="nenhum-resultado">Nenhum registro encontrado.</p>';
                }
                ?>
            </div>

    </div>  

    <script type="text/javascript">
    jQuery(document).ready(function() {
        jQuery('#id_menu_principal').change(function() {
            var idMenuPrincipal = jQuery(this).find('option:selected').val();
            var $containerLoad = jQuery('#container-load');
            if (idMenuPrincipal != '') {
                showLoad($containerLoad);
                jQuery.ajax({
                    type: 'GET',
                    url: "<?php echo site_url('admin/post/ajax_getMenus') ?>".concat('/', idMenuPrincipal),
                    success: function(html) {
                        jQuery('#menu').html(html).trigger('change');
                        hideLoad($containerLoad);
                        
                    },
                    error: function(err) {
                        hideLoad($containerLoad);
                        alert(err);
                    }
                });
            }
        });
        
          jQuery(document).ready(function() {
            jQuery('#menu').change(function() {
                var idMenu = jQuery(this).find('option:selected').val();
                var url = "<?php echo site_url('admin/post/index') ?>";
                window.location = url.concat('/', idMenu);
            });


        });
        
        
    });
    </script>







